#!/bin/bash

if [ `uname -s` != 'Linux' ];then
    printf \
    '
        {	
            "messageCode": "error",
            "message": "no support os type [os: '`uname -s`']",
            "data": []
        }
    '
    exit
fi

#基于Linux平台的NGINX自发现
function linux_nginx_check {
	#局部变量定义
	local result=""
	local err_msg=""
    
	#获取主机名
	function host_name {
		result=`uname -n 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_HOSTNAME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$result" ];then
				echo "$result"
				return 0
			else
				return 1
			fi
		fi
	}

	#判断脚本执行用户
	function check_user_permissive {
		if [ ${USER} == 'root' ];then
			priv="yes"
		elif [ ${USER} == "${run_user}" ];then
			priv="yes"
		elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
			priv="yes"
		else
			return_error 'NO_USER_PRIVILEGES'
			exit
		fi
	}
		
	#获取IP地址
	function ipaddr {
		if [ `uname` = "Linux" ];then
			if [ -f '/sbin/ip' ];then
				ips=(`/sbin/ip addr|awk 'BEGIN{FS="/| "};/global/&&!/secondary/{print $6}'|egrep -v '^169.*|^127.*|virbr|docker"'|sort`)
			elif [ -f '/bin/ip' ];then
				ips=(`/bin/ip addr|awk 'BEGIN{FS="/| "};/global/&&!/secondary/{print $6}'|egrep -v '^169.*|^127.*|virbr|docker"'|sort`)
			elif [ `which ip 2>/dev/null;echo $?` -eq 0 ];then
				ips=(`ip addr|awk 'BEGIN{FS="/| "};/global/&&!/secondary/{print $6}'|egrep -v '^169.*|^127.*|virbr|docker"'|sort`)
			else
				err_msg="IP_TOOL_NOT_FOUND"
			fi
		else
			err_msg="UNKNOWN_OS"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$ips" ];then
				echo ${ips[0]}
				return 0
			else
				return 1
			fi
		fi
	}
    
	#前端IP[必有，10.1,10.2,10.98开头，如有多个，则对应的IP与主机名写到/etc/hosts的为前端IP]
	function get_qdIp {
	    ip_src=`/sbin/ip addr 2>/dev/null|awk 'BEGIN{FS="/| "};/global/&&!/secondary/{print $6}'|egrep -v '^169.*|^127.*|^10.131.*'|sort |grep ^10|xargs`
		if [ $? -ne 0 ];then
			err_msg="GET_MANAGER_IP_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$ip_src" ];then
			    ip_arry=(${ip_src})
                if [ ${#ip_arry[*]} -eq 1 ];then
                    echo "${ip_arry}"
                else
                    for ((i=0;i<${#ip_arry[*]};i++))
                    do
                        if [[ $(grep -i `hostname` /etc/hosts |grep ${ip_arry[$i]} |grep -v \# |wc -l) -gt 0 ]];then
                            echo "${ip_arry[$i]}"
                            return 0
                        fi
                    done
                fi
			else
				return 1
			fi
		fi
	}
	
	#获取NGINX的二进制命令
	function get_nginx_cmd {
        nginx_exe=`ps -ef |grep ${nginx_pid} |awk '{print $11}'|grep sbin|head -1`
        if [ ! -z "${nginx_exe}" ];then
            nginx_cmd=${nginx_exe}
        else
            if [ ${USER} == 'root' ];then
                nginx_cmd=`ls -l /proc/${nginx_pid}/exe |awk '{print $NF}'`
            elif [ "${USER}" == "${run_user}" ];then
                nginx_cmd=`ls -l /proc/${nginx_pid}/exe |awk '{print $NF}'`
            elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
                nginx_cmd=`sudo -u "${run_user}" ls -l /proc/${nginx_pid}/exe |awk '{print $NF}'`
            else
                err_msg="NO_USER_PRIVILEGES" 
            fi
        fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_CMD_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$nginx_cmd" ];then
				echo "$nginx_cmd"
				return 0
			else
				echo "$err_msg"
				return 1
			fi
		fi
	}
	
	#获取NGINX端口
	function nginx_port {
        nginx_cmd=$(get_conf_dir)
        search_dir=${nginx_cmd%/sbin/nginx*}
		if [ ${USER} == 'root' ];then
			port=`netstat -nltup 2>/dev/null|grep ${nginx_pid}|awk '{print $4}'|awk -F: '{print $2}'|xargs echo|sed 's/ /,/g'`
            if [ -z "${port}" ];then
                find_conf=`find ${search_dir} -name nginx.conf 2>/dev/null |head -1`
                port=`grep listen ${find_conf} |grep -v "#" |awk '{print $2}'|sed 's/;//g'`
            fi
		elif [ ${USER} == "${run_user}" ];then
			port=`netstat -nltup 2>/dev/null|grep ${nginx_pid}|awk '{print $4}'|awk -F: '{print $2}'|xargs echo|sed 's/ /,/g'`
            if [ -z "${port}" ];then
                find_conf=`find ${search_dir} -name nginx.conf 2>/dev/null |head -1`
                port=`grep listen ${find_conf} |grep -v "#" |awk '{print $2}'|sed 's/;//g'`
            fi
		elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
			#port=`sudo -u "${run_user}" netstat -nltup 2>/dev/null |grep ${nginx_pid} |awk '{print $4}'|awk -F: '{print $2}'|xargs echo|sed 's/ /,/g'`
            get_nginx_pid_arry=`ps -ef |grep ${nginx_pid} |grep -v grep |awk '{print $2}' |xargs`
            for pid in ${get_nginx_pid_arry}
            do
                port=`sudo -u "${run_user}" netstat -nltup 2>/dev/null |grep ${pid} |awk '{print $4}'|awk -F: '{print $2}'|xargs echo|sed 's/ /,/g'`
                if [ ! -z "${port}" ];then
                    #echo ${port}
                    break
                fi
            done
            if [ -z "${port}" ];then
                find_conf=`sudo -u "${run_user}" find ${search_dir} -name nginx.conf 2>/dev/null |grep nginx |head -1`
                port=`sudo -u "${run_user}" grep listen ${find_conf} |grep -v "#" |awk '{print $2}'|sed 's/;//g'`
            fi
		else
			err_msg="GET_NGINX_PORT_NO_COMPETENCE"
		fi

		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_PORT_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "${port}" ];then
                echo "${port}"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取NGINX版本
	function version {
        #run_user=`ps -ef |grep nginx |grep master |grep -v grep|awk '{print $1}'`
		nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			if [ "$nginx_cmd" == 'GET_NGINX_CMD_NO_COMPETENCE' ];then
				err_msg="GET_NGINX_VERSION_NO_COMPETENCE"
			else
				err_msg="GET_NGINX_VERSION_FAILURE"			
			fi
		else
			if [ ${USER} == 'root' ];then
				version=`$nginx_cmd -v 2>&1 |grep version |awk -F "/" '{print $NF}'`
			elif [ ${USER} == "${run_user}" ];then
				version=`$nginx_cmd -v 2>&1 |grep version |awk -F "/" '{print $NF}'`
			elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
				version=`sudo -u ${run_user} $nginx_cmd -v 2>&1 |grep version |awk -F "/" '{print $NF}'`
			else
				err_msg="NO_USER_PRIVILEGES" 
			fi
		fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_VERSION_FAILURE"
		fi			
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ `echo $version |grep "^[1-9]" |wc -l` -gt 0 ];then
				echo "$version"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取操作系统
	function os {
		os=`uname 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_OS_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$os" ];then
				echo "$os"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取用户shell
	function shell {
		shell=`echo $SHELL 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_SHELL_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$shell" ];then
				echo "$shell"
				return 0
			else
				return 1
			fi
		fi	
	}
	
	#获取NGINX模块
	function get_module {
		nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			if [ "$nginx_cmd" = 'GET_NGINX_CMD_NO_COMPETENCE' ];then
				err_msg="GET_NGINX_MODULE_NO_COMPETENCE"
			else
				err_msg="GET_NGINX_MODULE_FAILURE"			
			fi
		else
			if [ ${USER} == 'root' ];then
				module=`$nginx_cmd -V 2>&1|grep 'configure arguments'`			
			elif [ ${USER} == "${run_user}" ];then
				module=`$nginx_cmd -V 2>&1|grep 'configure arguments'`
            elif [ `sudo -Al 2>/dev/null |grep ${run_user} |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
                module=`sudo -u ${run_user} $nginx_cmd -V 2>&1|grep 'configure arguments'`
			else
				err_msg="GET_NGINX_MODULE_NO_COMPETENCE"	
			fi
		fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_MODULE_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$module" ];then
				echo "$module"|sed 's/--/\n--/g' |egrep "with|module" |sed ':a;N;$!ba;s/\n/ /g'
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取NGINX安装目录或NGINX_APPLICATION_HOME
	function get_install_dir {
		nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			if [ "$nginx_cmd" = 'GET_NGINX_CMD_NO_COMPETENCE' ];then
				err_msg="GET_NGINX_INSTALL_DIR_NO_COMPETENCE"
			else
				err_msg="GET_NGINX_INSTALL_DIR_FAILURE"
			fi
		else
			if [ ${USER} == 'root' ];then
				install_dir=`$nginx_cmd  -V 2>&1|grep 'configure arguments' |sed 's/--/\n--/g' |grep '\--prefix='|awk -F = '{print $2}'`
                if [ -z "${install_dir}" ];then
                    install_dir=${nginx_cmd%/nginx/sbin/nginx}
                fi				
			elif [ ${USER} == "${run_user}" ];then
				install_dir=`$nginx_cmd  -V 2>&1|grep 'configure arguments' |sed 's/--/\n--/g' |grep '\--prefix='|awk -F = '{print $2}'`
                if [ -z "${install_dir}" ];then
                    install_dir=${nginx_cmd%/nginx/sbin/nginx}
                fi                
			elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
                install_dir=`sudo -u "${run_user}" $nginx_cmd  -V 2>&1|grep 'configure arguments' |sed 's/--/\n--/g' |grep '\--prefix='|awk -F = '{print $2}'`
                if [ -z "${install_dir}" ];then
                    install_dir=${nginx_cmd%/nginx/sbin/nginx}
                fi
            else
				err_msg="GET_NGINX_INSTALL_DIR_FAIL"
			fi
		fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_INSTALL_DIR_FAILURE"
		fi		
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "${install_dir}" ];then
				echo "${install_dir}"
				return 0
			else
				return 1
			fi
		fi		
	}

	#获取主键属性
	function get_key {
		ip=$(ipaddr)
		dir=$(get_install_dir)		
		if [[ "x$dir" = "x" ]] || [[ "x$ip" = "x" ]];then
			return 1
		else
			key=$(echo `ipaddr`_`get_install_dir`|md5sum|cut -c 1-6)
		fi
		echo "${ip}_${key}"
	}

	#获取NGINX起停目录
	function get_restart_dir {
		nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_RESTART_DIR_FAILURE"
		else
			restart_dir=`echo $nginx_cmd|xargs dirname`
			if [ $? -ne 0 ];then
				err_msg="GET_NGINX_RESTART_DIR_FAILURE"
			fi		
		fi		
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$restart_dir" ];then
				echo "$restart_dir"
				return 0
			else
				return 1
			fi
		fi		
	}
	
	#获取NGINX配置文件路径
	function get_conf_dir {
		get_proc=`ps -ef |grep ${nginx_pid}|grep master|grep -v grep 2>/dev/null`
        if [ "${get_proc}" ];then
            #nginx未指定配置文件启动,配置文件路径为默认路径
            if [ `ps -ef |grep ${nginx_pid} |grep master |grep "\-c" |wc -l` -eq 0 ];then
                nginx_cmd=`get_nginx_cmd`
                if [ `echo ${nginx_cmd}|grep sbin|wc -l` -gt 0 ];then
                    nginx_base=${nginx_cmd%/sbin*}
                    conf_dir=${nginx_base}/conf
                else
                    err_msg='GET_NGINX_CONF_FAILUE'
                    return 1
                fi
            else
            #nginx指定配置文件启动
                confd=`ps -ef |grep ${nginx_pid} |grep master |grep "\-c" |grep -oP '\-c.*\s' |awk '{print $2}'|xargs`
                if [ ! -z "${confd}" ];then
                    conf_dir=${confd}
                else
                    err_msg='GET_NGINX_CONF_FAILUE'
                    return 1
                fi
            fi
        else 
            err_msg="GET_NGINX_CONF_DIR_FAILURE"
        fi
        
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_CONF_DIR_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
            if [ "${conf_dir}" ];then
                echo "${conf_dir}"
            else
                return 1
            fi	
		fi			
	}
    
	#gcc依赖包版本
	function gcc_version {
	    nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			if [ "$nginx_cmd" = 'GET_NGINX_CMD_NO_COMPETENCE' ];then
				err_msg="GET_NGINX_VERSION_NO_COMPETENCE"
			else
				err_msg="GET_NGINX_VERSION_FAILURE"			
			fi
		else
			if [ ${USER} == 'root' ];then
				Details=`$nginx_cmd -V 2>&1`
			elif [ ${USER} == "${run_user}" ];then
				Details=`$nginx_cmd -V 2>&1`
			elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
				Details=`sudo -u "${run_user}" $nginx_cmd -V 2>&1`
			else
				err_msg="GET_NGINX_VERSION_NO_COMPETENCE"
			fi
		fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_VERSION_FAILURE"
		fi			
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$Details" ];then
				echo "$Details"|awk -F gcc '{print $2}'|awk '{print $1}'|xargs
				return 0
			else
				return 1
			fi
		fi
	}
    
	#Configure参数
	function configure {
		nginx_cmd=$(get_nginx_cmd)
		if [ $? -ne 0 ];then
			if [ "$nginx_cmd" = 'GET_NGINX_CMD_NO_COMPETENCE' ];then
				err_msg="GET_NGINX_VERSION_NO_COMPETENCE"
			else
				err_msg="GET_NGINX_VERSION_FAILURE"			
			fi
		else
			if [ ${USER} == 'root' ];then
				Details=`$nginx_cmd -V 2>&1`
			elif [ ${USER} == "${run_user}" ];then
				Details=`$nginx_cmd -V 2>&1`
			elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
				Details=`sudo -u "${run_user}" $nginx_cmd -V 2>&1`
			else
				err_msg="GET_NGINX_VERSION_NO_COMPETENCE"
			fi
		fi
		if [ $? -ne 0 ];then
			err_msg="GET_NGINX_VERSION_FAILURE"
		fi			
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$Details" ];then
				echo "$Details"|awk -F 'configure arguments:' '{print $2}'|sed ':a;N;$!ba;s/\n/ /g'
				return 0
			else
				return 1
			fi
		fi	
	}
	
	#启动命令
	function start_command {
	    command=`ps -ef |grep ${nginx_pid}|awk -F process '{print $2}'|xargs`
	    if [ $? -ne 0 ];then
		    err_msg="GET_START_COMMAND_FAILURE"
	    fi
        if [ "$1" = 'err_msg' ];then
            echo "$err_msg"
            return 1
        else
            if [ "$command" ];then
                echo "$command"
                return 0
            else
                return 1
            fi
        fi
	}
	
	#进程数
	function processors {
        command=`ps -ef |grep ${nginx_pid} |grep process |grep -v grep|wc -l`
        if [ $? -ne 0 ];then
            err_msg="GET_START_COMMAND_FAILURE"
        fi
        if [ "$1" = 'err_msg' ];then
                echo "$err_msg"
                return 1
        else
            if [ "$command" ];then
                echo "$command"
                return 0
            else
                return 1
            fi
        fi
	}
	
	#worker_connections数 
	function work_connection {
        nginx_cmd=$(get_conf_dir)
        search_dir=${nginx_cmd%/nginx/conf*}
        if [ $? -ne 0 ];then
            if [ "$nginx_cmd" = 'GET_NGINX_CMD_NO_COMPETENCE' ];then
                err_msg="GET_NGINX_CONNECTIONS_NO_COMPETENCE"
            else
                err_msg="GET_NGINX_CONNECTIONS_FAIL"			
            fi
        else
            if [ ${USER} == 'root' ];then
                find_conf=`find ${search_dir} -name nginx.conf 2>/dev/null |head -1`
                if [ -e "${find_conf}" ];then
                    Details=`grep -r worker_connections ${find_conf} |grep -v \# |awk '{print $2}'|awk -F ';' '{print $1}'|xargs`
                else
                    err_msg="GET_CONF_FAILUE"
                fi
            elif [ ${USER} == "${run_user}" ];then
                find_conf=`find ${search_dir} -name nginx.conf 2>/dev/null |head -1`
                if [ -e "${find_conf}" ];then
                    Details=`grep -r worker_connections ${find_conf} |grep -v \# |awk '{print $2}'|awk -F ';' '{print $1}'|xargs`
                else
                    err_msg="GET_CONF_FAILUE"
                fi
            elif [ `sudo -Al 2>/dev/null |grep "${run_user}" |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
                find_conf=`sudo -u "${run_user}" find ${search_dir} -name nginx.conf 2>/dev/null |head -1`
                if [ -e "${find_conf}" ];then
                    Details=`sudo -u "${run_user}" grep -r worker_connections ${find_conf} |grep -v \# |awk '{print $2}'|awk -F ';' '{print $1}'|xargs`
                else
                    err_msg="GET_CONF_FAILUE"
                fi
            else
                err_msg="NO_USER_PRIVILEGES" 
            fi
        fi
        if [ $? -ne 0 ];then
            err_msg="GET_NGINX_VERSION_FAILURE"
        fi	
        if [ "$1" = 'err_msg' ];then
            echo "$err_msg"
            return 1
        else
            if [ "$Details" ];then
                echo "$Details" |awk '{print $1}'
                return 0
            else
                return 1
            fi
        fi	
	}

    #资产发现方式0-远程扫描，1-WEB爬虫，2-网络流量分析，3-日志分析，4-Agent，5-登录资产，6-手动导入，7-其他
    function found_typelist() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "Agent"
				return 0
		fi
    }
    #资产发现时间
    function found_type_time() {
        now_time=`date '+%Y%m%d%H%M%S'`
		if [ $? -ne 0 ];then
			err_msg="GET_NOW_TIME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$now_time" ];then
				echo "$now_time"
				return 0
			else
				return 1
			fi
		fi

    }
    #是否上报,是、否、待上报
    function is_report() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "待上报"
				return 0
		fi
    }
	
	#是否打印逗号
	function print_comma {
		if [ $1 -ne $2 ]; then
			echo ","
		fi
	}

	#打印结果
	function return_result {
        
		pidd=(`ps -ef |grep nginx |grep master |awk '{print $2}'|xargs`)
		num=`expr ${#pidd[*]} - 1`

        #判断当前用户权限
        for ppid in ${pidd[*]}
        do
            run_user=`ps -ef |grep ${ppid}|grep -v grep |awk '{print $1}'|head -1`
            if [ ${USER} == 'root' ];then
                priv='yes'
            elif [ "${USER}" == "${run_user}" ];then
                priv='yes'
            elif [ `sudo -Al 2>/dev/null |grep ${run_user} |wc -l` -gt 0 -o `sudo -Al 2>/dev/null |grep "(ALL) NOPASSWD" |wc -l` -gt 0 ];then
                priv='yes'
            else
                return_error 'NO_USER_PRIVILEGES'
                exit
            fi
        done
            
		printf \
		'
		{	
			"messageCode": "success",
			"message": "",
			"data": [
		'
        
		#打印每个实例
		for ((i=0;i<${#pidd[*]};i++))
		do
            nginx_pid=${pidd[$i]}
            run_user=`ps -ef|grep ${nginx_pid}|grep master|grep -v grep |awk '{print $1}'|head -1`
            
			echo \
			'{
				"ciCode": "nginx_instance",
				"attributes": [
					{
						"attrName": "主键",
					    "attrCode": "nginx_key",
					    "attrValue": "'`get_key`'",
					    "attrFailureReason": "''"
					},{
						"attrName": "监听地址",
					    "attrCode": "ip",
					    "attrValue": "'`ipaddr`'",
					    "attrFailureReason": "'`ipaddr err_msg`'"
					},{
						"attrName": "版本",
						"attrCode": "version",
						"attrValue": "'`version`'",
						"attrFailureReason": "'`version err_msg`'"		
					},{
						"attrName": "主机名",
						"attrCode": "host_name",
						"attrValue": "'`host_name`'",
						"attrFailureReason": "'`host_name err_msg`'"		
					},{
						"attrName": "操作系统",
						"attrCode": "operating_system",
						"attrValue": "'`os`'",
						"attrFailureReason": "'`os err_msg`'"		
					},{
						"attrName": "启动用户",
						"attrCode": "user_name",
						"attrValue": "'`echo ${run_user}`'",
						"attrFailureReason": ""		
					},{
						"attrName": "用户shell",
						"attrCode": "user_shell",
						"attrValue": "'`shell`'",
						"attrFailureReason": "'`shell err_msg`'"		
					},{
						"attrName": "监听端口",
						"attrCode": "port",
						"attrValue": "'`nginx_port`'",
						"attrFailureReason": "'`nginx_port err_msg`'"		
					},{
						"attrName": "nginx模块",
						"attrCode": "nginx_module",
						"attrValue": "'`get_module`'",
						"attrFailureReason": "'`get_module err_msg`'"		
					},{
						"attrName": "安装目录",
						"attrCode": "installation_directory",
						"attrValue": "'`get_install_dir`'",
						"attrFailureReason": "'`get_install_dir err_msg`'"		
					},{
						"attrName": "起停目录",
						"attrCode": "start_stop_list",
						"attrValue": "'`get_restart_dir`'",
						"attrFailureReason": "'`get_restart_dir err_msg`'"		
					},{
						"attrName": "配置文件路径",
						"attrCode": "profile_path",
						"attrValue": "'`get_conf_dir`'",
						"attrFailureReason": "'`get_conf_dir err_msg`'"		
					},{
						"attrName": "APPLICATION_HOME",
						"attrCode": "application_home",
						"attrValue": "'`get_install_dir`'",
						"attrFailureReason": "'`get_install_dir err_msg`'"		
					},{
						"attrName": "依赖包版本",
						"attrCode": "gcc_version",
						"attrValue": "'`gcc_version`'",
						"attrFailureReason": "'`gcc_version err_msg`'"
					},{
						"attrName": "Configure参数",
						"attrCode": "configure",
						"attrValue": "'`configure`'",
						"attrFailureReason": "'`configure err_msg`'"
					},{
						"attrName": "启动命令",
						"attrCode": "start_command",
						"attrValue": "'`start_command`'",
						"attrFailureReason": "'`start_command err_msg`'"
					},{
						"attrName": "进程数",
						"attrCode": "processors",
						"attrValue": "'`processors`'",
						"attrFailureReason": "'`processors err_msg`'"
					},{
						"attrName": "worker_connections数",
						"attrCode": "work_connection",
						"attrValue": "'`work_connection`'",
						"attrFailureReason": "'`work_connection err_msg`'"
					},{
						"attrName": "资产发现方式",
						"attrCode": "found_typelist",
						"attrValue": "'`found_typelist`'",
						"attrFailureReason": "'`found_typelist err_msg`'"		
					},{
						"attrName": "资产发现时间",
						"attrCode": "found_type_time",
						"attrValue": "'`found_type_time`'",
						"attrFailureReason": "'`found_type_time err_msg`'"		
					},{
						"attrName": "是否上报",
						"attrCode": "is_report",
						"attrValue": "'`is_report`'",
						"attrFailureReason": "'`is_report err_msg`'"		
					}
				],
				"relations": [
					{
						"quoteCiCode": "host",
						"relationName": "运行于",
						"quoteInstances": [
							[
								{
									"attrName": "运行于主机的IP",
									"attrCode": "ip",
									"attrValue": "'`ipaddr`'",
									"attrFailureReason": "'`ipaddr err_msg`'"
								}
							]
						]
					}
				]
			}'`print_comma $i $num`''
		done
		echo "]}"
	}
	return_result;
}

#错误定义
function return_error {
	case $1 in 
	'UNKNOW_OS')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "no support os type [os: '`uname -s`']",
				"data": []
			}
		'
		;;
	'NO_INSTANCE')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "no instance",
				"data": []
			}
		'
		;;
	'CMD_EXECUTE_ERROR')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "environment not ready [ps exec error]",
				"data": []
			}
		'
		;;
	'NO_USER_PRIVILEGES')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "no user privileges [current: '`echo ${run_user}`']",
				"data": []
			}
		'
		;;
	esac
}

#自发现入口
function main {
	os_type=`uname -s`
	case ${os_type} in
		'Linux')
			if [ `ps -ef | grep nginx |grep master | grep -v grep | wc -l` -gt 0 ] && [ $? -eq 0 ]; then
				linux_nginx_check;	
			elif [ `ps -ef | grep nginx |grep master | grep -v grep | wc -l` -gt 0 ] && [ $? -ne 0 ]; then
				return_error 'CMD_EXECUTE_ERROR';
			else
				return_error 'NO_INSTANCE';
			fi
			;;
		'*')
			return_error 'UNKNOW_OS';
			;;
	esac
}

main;
